const express = require('express')
// const crypto = require('crypto')
const validate = require('express-validation')
const validation = require('../validation')
const _ = require('lodash')
// const moment = require('moment')
const kindController = require('../controller/kindController')
const tokenChecker = require('../util/tokenChecker')

const kindRouter = express.Router()

kindRouter.post('/add', tokenChecker, validate(validation.shopAddKinds), async (req, res, next) => {
  const {
    kinds
  } = req.body
  try {
    // tokenChecker根据token返回uuId
    const targetShopId = req.uuId
    _.forEach(kinds, (item) => {
      item.shopId = targetShopId
    })
    const resultKinds = await kindController.createKind(kinds)
    res.json({
      status: 200,
      msg: '保存分类成功',
      data: {
        kinds: resultKinds
      }
    })
  } catch (e) {
    next(e)
  }
})

kindRouter.post('/delete', tokenChecker, async (req, res, next) => {
  // 每次只能添加或更新一个商品
  // [{id:''}]
  const {
    kinds
  } = req.body
  try {
    const result = await kindController.delete(kinds)
    // if (!result) {
    //   res.json({ status: 200, msg: '保存商品成功', data: {} })
    // }
    res.json({
      status: 200,
      msg: '删除分类成功',
      data: {}
    })
  } catch (e) {
    next(e)
  }
})

module.exports = kindRouter